package com.le.biz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.le.bean.Reply;
import com.le.dao.ReplyDao;
import com.le.web.model.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(
        propagation = Propagation.REQUIRED,
        isolation = Isolation.DEFAULT,
        timeout = 2000,
        rollbackFor = RuntimeException.class
)
@Slf4j
public class ReplyBizImpl implements ReplyBiz{
    @Autowired
    private ReplyDao replyDao;

    @Transactional(
            propagation = Propagation.SUPPORTS,
            isolation = Isolation.DEFAULT,
            timeout = 2000,
            readOnly = true,
            rollbackFor = RuntimeException.class
    )
    @Override
    public PageBean<Reply> findByPage(int pageno, int pagesize, int topicid) {
        int start = (pageno - 1) * pagesize; // 分页的起始位置

        List<Reply> list = this.replyDao.findByPage(start, pagesize, topicid);
        // 包装分页Bean
        PageBean replyPage = new PageBean();
        replyPage.setPageno(pageno);
        replyPage.setPagesize(pagesize);
        replyPage.setDataset(list);
        // 查询boardid下总的主题的数量
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("topicid", topicid);
        Integer total = this.replyDao.selectCount(wrapper);
        replyPage.setTotal(total);
        // 计算总页数   ： 总记录数/pagesize
        int totalpages = total%pagesize==0? total/pagesize: total/pagesize+1;
        replyPage.setTotalpages(totalpages);

        if (replyPage.getPageno() <= 1){
            replyPage.setPre(1);
        } else {
            replyPage.setPre(replyPage.getPageno() - 1);
        }
        // 下一页的页号
        if (replyPage.getPageno() == totalpages){
            replyPage.setNext(totalpages);
        } else {
            replyPage.setNext(replyPage.getPageno() + 1);
        }
        return replyPage;
    }

    @Override
    public void deleteReplyById(Integer replyid) {
        this.replyDao.deleteById(replyid);
    }

    @Override
    public void addReply(Reply reply) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
        String format = sdf.format(date);
        reply.setPublishtime(format);
        reply.setModifytime(format);
        this.replyDao.insert(reply);
    }

    @Override
    public void updateReply(Reply reply){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
        String format = sdf.format(date);
        reply.setModifytime(format);
        this.replyDao.updateById(reply);
    }

    @Transactional(
            propagation = Propagation.SUPPORTS,
            isolation = Isolation.DEFAULT,
            timeout = 2000,
            readOnly = true,
            rollbackFor = RuntimeException.class
    )
    @Override
    public int isReplyId(Integer replyid) {
        Reply reply = this.replyDao.selectById(replyid);
        if (reply == null){
            return 0;
        }
        return 1;
    }
}
